package xyz.xiangyushui.controller.membership.profile;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import xyz.xiangyushui.daoFactory.user.UserDaoFactory;
import xyz.xiangyushui.javaBean.user.User;

/**
 * Servlet implementation class UpdataePwd
 */
@WebServlet("/member/profile/updatePwd")
public class UpdataePwd extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private UserDaoFactory f;
    
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdataePwd() {
        super();
        f=new UserDaoFactory();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String pwd=request.getParameter("old-pwd");
		String pwdNew=request.getParameter("new-pwd");
		String userName=(String)request.getSession(false).getAttribute("userName");
		User u=f.getUserDao().auth(userName, pwd);
		if(u==null){
			request.setAttribute("failedMsg", "wrong password!");
		}else{
			f.getUserDao().modifyPwd(userName, pwdNew);
			request.setAttribute("successMsg", "modify password successfully!");
		}
		request.getRequestDispatcher("/member/profile").forward(request, response);
	}

}
